package cn.innosmart.aq.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.AquariumBean;
import cn.innosmart.aq.bean.DeviceBean;
import cn.innosmart.aq.util.DeviceConstant;
import cn.innosmart.aq.util.SystemConstant;

/**
 * Deprecated by David  replaced by GridView
 * Created by David on 2015/7/28.
 */
public class DeviceItemsListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context context;
    private ArrayList<String> items;

    public DeviceItemsListAdapter(Context context) {
        this.context = context;
        items = new ArrayList<String>();
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_other_items, null);
            holder.tv_device_name = (TextView) convertView.findViewById(R.id.tv_device_name);
            holder.tv_last_flag = (TextView) convertView.findViewById(R.id.tv_last_flag);
            holder.tv_top_content = (TextView) convertView.findViewById(R.id.tv_top_content);
            holder.tv_next_flag = (TextView) convertView.findViewById(R.id.tv_next_flag);
            holder.tv_bottom_content = (TextView) convertView.findViewById(R.id.tv_bottom_content);
            holder.iv_history_list = (ImageView) convertView.findViewById(R.id.iv_history_list);
            holder.mTogBtn = (ToggleButton) convertView.findViewById(R.id.mTogBtn);
            holder.tv_button = (TextView) convertView.findViewById(R.id.tv_button);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(items.get(position));
        if (deviceBean != null) {
            if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_WATERLEVER) {
                //0正常  1低 2高
                holder.tv_device_name.setText(context.getString(R.string.listview_devicelist_water_level_title));
                holder.tv_last_flag.setText("水位显示:");
                holder.tv_next_flag.setVisibility(View.GONE);
                holder.tv_bottom_content.setVisibility(View.GONE);
//                holder.tv_next_flag.setText("上次换水:");
                holder.mTogBtn.setVisibility(View.INVISIBLE);
                holder.tv_button.setVisibility(View.INVISIBLE);
//                holder.tv_button.setText("换水");
                switch ((int) deviceBean.getSensorvalue()) {
                    case 0:
                        holder.tv_top_content.setText("正常");
                        break;
                    case 1:
                        holder.tv_top_content.setText("偏低");
                        break;
                    case 2:
                        holder.tv_top_content.setText("偏高");
                        break;
                }
            } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_SWITCH) {
                //此处模拟成灯光插座
                double basicvalue = deviceBean.getSensorvalue();
                if (TextUtils.isEmpty(deviceBean.getLabel())) {
                    holder.tv_device_name.setText("插座");
                    holder.tv_last_flag.setText("开关");
                    holder.tv_next_flag.setVisibility(View.GONE);
                    holder.tv_bottom_content.setVisibility(View.GONE);
                    holder.tv_top_content.setVisibility(View.GONE);
                    holder.tv_bottom_content.setVisibility(View.GONE);
                    holder.mTogBtn.setVisibility(View.VISIBLE);
                    holder.tv_button.setVisibility(View.INVISIBLE);
                    if (basicvalue == 0) {
                        holder.mTogBtn.setChecked(false);
                    } else if (basicvalue == 255) {
                        holder.mTogBtn.setChecked(true);
                    }
                    holder.mTogBtn.setTag(deviceBean);
                    holder.mTogBtn.setOnClickListener(onClickListener);
                } else {
                    if (deviceBean.getLabel().equals("Switch")) {
                        String userTag = deviceBean.getUserTag();
                        if (userTag != null) {
                            if (userTag.equals("Feed Switch")) {
                                holder.tv_device_name.setText("喂食");
                                holder.tv_last_flag.setText("上次喂食:");
                                holder.tv_next_flag.setText("下次喂食:");
                                holder.tv_top_content.setText(context.getString(R.string.listview_value_unset));
                                holder.tv_bottom_content.setText(context.getString(R.string.listview_value_unset));
                                holder.mTogBtn.setVisibility(View.VISIBLE);
                                holder.tv_button.setVisibility(View.INVISIBLE);
                                if (basicvalue == 0) {
                                    holder.mTogBtn.setChecked(false);
                                } else if (basicvalue == 255) {
                                    holder.mTogBtn.setChecked(true);
                                }
                                holder.mTogBtn.setTag(deviceBean);
                                holder.mTogBtn.setOnClickListener(onClickListener);
                            } else if (userTag.equals("Water Switch")) {
                                holder.tv_device_name.setText("换水");
                                holder.tv_last_flag.setText("上次换水:");
                                holder.tv_next_flag.setText("下次换水:");
                                holder.tv_top_content.setText(context.getString(R.string.listview_value_unset));
                                holder.tv_bottom_content.setText(context.getString(R.string.listview_value_unset));
                                holder.mTogBtn.setVisibility(View.VISIBLE);
                                holder.tv_button.setVisibility(View.INVISIBLE);
                                if (basicvalue == 0) {
                                    holder.mTogBtn.setChecked(false);
                                } else if (basicvalue == 255) {
                                    holder.mTogBtn.setChecked(true);
                                }
                                holder.mTogBtn.setTag(deviceBean);
                                holder.mTogBtn.setOnClickListener(onClickListener);
                            } else if (userTag.equals("O2 Switch")) {
                                holder.tv_device_name.setText("氧气");
                                holder.tv_last_flag.setText("上次增氧:");
                                holder.tv_next_flag.setText("下次增氧:");
                                holder.tv_top_content.setText(context.getString(R.string.listview_value_unset));
                                holder.tv_bottom_content.setText(context.getString(R.string.listview_value_unset));
                                holder.mTogBtn.setVisibility(View.VISIBLE);
                                holder.tv_button.setVisibility(View.INVISIBLE);
                                if (basicvalue == 0) {
                                    holder.mTogBtn.setChecked(false);
                                } else if (basicvalue == 255) {
                                    holder.mTogBtn.setChecked(true);
                                }
                                holder.mTogBtn.setTag(deviceBean);
                                holder.mTogBtn.setOnClickListener(onClickListener);
                            } else if (userTag.equals("Light Switch")) {
                                holder.tv_device_name.setText("照明");
                                holder.tv_last_flag.setText("上次照明:");
                                holder.tv_next_flag.setText("下次照明:");
                                holder.tv_top_content.setText(context.getString(R.string.listview_value_unset));
                                holder.tv_bottom_content.setText(context.getString(R.string.listview_value_unset));
                                holder.mTogBtn.setVisibility(View.VISIBLE);
                                holder.tv_button.setVisibility(View.INVISIBLE);
                                if (basicvalue == 0) {
                                    holder.mTogBtn.setChecked(false);
                                } else if (basicvalue == 255) {
                                    holder.mTogBtn.setChecked(true);
                                }
                                holder.mTogBtn.setTag(deviceBean);
                                holder.mTogBtn.setOnClickListener(onClickListener);
                            }
                        } else {
                            holder.tv_device_name.setText("插座");
                            holder.tv_last_flag.setText("开关");
                            holder.tv_next_flag.setVisibility(View.GONE);
                            holder.tv_bottom_content.setVisibility(View.GONE);
                            holder.tv_top_content.setVisibility(View.GONE);
                            holder.tv_bottom_content.setVisibility(View.GONE);
                            holder.mTogBtn.setVisibility(View.VISIBLE);
                            holder.tv_button.setVisibility(View.INVISIBLE);
                            if (basicvalue == 0) {
                                holder.mTogBtn.setChecked(false);
                            } else if (basicvalue == 255) {
                                holder.mTogBtn.setChecked(true);
                            }
                            holder.mTogBtn.setTag(deviceBean);
                            holder.mTogBtn.setOnClickListener(onClickListener);
                        }
//                holder.tv_button.setText("换水");
                    } else if (deviceBean.getLabel().equals("Feed Switch")) {
                        holder.tv_device_name.setText("喂食");
                        holder.tv_last_flag.setText("上次喂食:");
                        holder.tv_next_flag.setText("下次喂食:");
                        holder.tv_top_content.setText(context.getString(R.string.listview_value_unset));
                        holder.tv_bottom_content.setText(context.getString(R.string.listview_value_unset));
                        holder.mTogBtn.setVisibility(View.VISIBLE);
                        holder.tv_button.setVisibility(View.INVISIBLE);
                        if (basicvalue == 0) {
                            holder.mTogBtn.setChecked(false);
                        } else if (basicvalue == 255) {
                            holder.mTogBtn.setChecked(true);
                        }
                        holder.mTogBtn.setTag(deviceBean);
                        holder.mTogBtn.setOnClickListener(onClickListener);
                    } else if (deviceBean.getLabel().equals("Water Switch")) {
                        holder.tv_device_name.setText("换水");
                        holder.tv_last_flag.setText("上次换水:");
                        holder.tv_next_flag.setText("下次换水:");
                        holder.tv_top_content.setText(context.getString(R.string.listview_value_unset));
                        holder.tv_bottom_content.setText(context.getString(R.string.listview_value_unset));
                        holder.mTogBtn.setVisibility(View.VISIBLE);
                        holder.tv_button.setVisibility(View.INVISIBLE);
                        if (basicvalue == 0) {
                            holder.mTogBtn.setChecked(false);
                        } else if (basicvalue == 255) {
                            holder.mTogBtn.setChecked(true);
                        }
                        holder.mTogBtn.setTag(deviceBean);
                        holder.mTogBtn.setOnClickListener(onClickListener);
                    } else if (deviceBean.getLabel().equals("O2 Switch")) {
                        holder.tv_device_name.setText("氧气");
                        holder.tv_last_flag.setText("上次增氧:");
                        holder.tv_next_flag.setText("下次增氧:");
                        holder.tv_top_content.setText(context.getString(R.string.listview_value_unset));
                        holder.tv_bottom_content.setText(context.getString(R.string.listview_value_unset));
                        holder.mTogBtn.setVisibility(View.VISIBLE);
                        holder.tv_button.setVisibility(View.INVISIBLE);
                        if (basicvalue == 0) {
                            holder.mTogBtn.setChecked(false);
                        } else if (basicvalue == 255) {
                            holder.mTogBtn.setChecked(true);
                        }
                        holder.mTogBtn.setTag(deviceBean);
                        holder.mTogBtn.setOnClickListener(onClickListener);
                    } else if (deviceBean.getLabel().equals("Light Switch")) {
                        holder.tv_device_name.setText("照明");
                        holder.tv_last_flag.setText("上次照明:");
                        holder.tv_next_flag.setText("下次照明:");
                        holder.tv_top_content.setText(context.getString(R.string.listview_value_unset));
                        holder.tv_bottom_content.setText(context.getString(R.string.listview_value_unset));
                        holder.mTogBtn.setVisibility(View.VISIBLE);
                        holder.tv_button.setVisibility(View.INVISIBLE);
                        if (basicvalue == 0) {
                            holder.mTogBtn.setChecked(false);
                        } else if (basicvalue == 255) {
                            holder.mTogBtn.setChecked(true);
                        }
                        holder.mTogBtn.setTag(deviceBean);
                        holder.mTogBtn.setOnClickListener(onClickListener);
                    }
                }
            } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_FEEDER) {
                double basicvalue = deviceBean.getSensorvalue();
                holder.tv_device_name.setText(R.string.listview_devicelist_feeder_title);
                holder.tv_last_flag.setText("上次喂食:");
                holder.tv_next_flag.setText("下次喂食:");
                holder.tv_top_content.setText("未知");
                holder.tv_bottom_content.setText("未知");
                holder.mTogBtn.setVisibility(View.INVISIBLE);
                holder.tv_button.setVisibility(View.VISIBLE);
                if (basicvalue == 255) {
                    holder.tv_button.setText("正在喂食");
                    holder.tv_button.setEnabled(false);
                } else {
                    holder.tv_button.setText("喂食");
                    holder.tv_button.setEnabled(true);
                }
                holder.tv_button.setTag(deviceBean);
                holder.tv_button.setOnClickListener(onClickListener);
            }
            //Light Switch Feed Switch  Water Switch O2 Switch
            holder.iv_history_list.setTag(deviceBean);
            holder.iv_history_list.setOnClickListener(onClickListener);
        }

        return convertView;
    }

    static class ViewHolder {
        TextView tv_device_name;
        TextView tv_last_flag;
        TextView tv_top_content;
        TextView tv_next_flag;
        TextView tv_bottom_content;
        ImageView iv_history_list;
        ToggleButton mTogBtn;
        TextView tv_button;
    }

    private View.OnClickListener onClickListener;

    public void setClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    private AquariumBean aquariumBean;

    public void refreshData(AquariumBean aquariumBean) {
        this.aquariumBean = aquariumBean;
        items = aquariumBean.getOtherDevices();
        notifyDataSetChanged();
    }
}
